<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ site.title }}</title>
    {{include 'components/import'}}
    <script src="/javascripts/validator.min.js"></script>
</head>
<body>
    <!-- {{include 'components/site-nav'}} -->
    {{include 'components/header'}}
    <div class="container-fluid login">
        <div class="container">
          <img src="/images/login.jpg">
          <div class="con">
            <form class="form-horizontal" role="form" id="loginForm">
              <div class="form-group">
                <div class="col-xs-12">
                  <input type="text" class="form-control" id="username" placeholder="用户名" required>
                </div>
              </div>
              <div class="form-group">
                <div class="col-xs-12">
                  <input type="password" class="form-control" id="password" placeholder="登录密码" required>
                </div>
              </div>
              <div class="form-group">
                <div class="col-xs-6">
                  <a href="javascript:signin();" class="btn btn-theme">登录</a>
                  <a href="/{{ site.subDomain }}/register" class="btn">注册</a>
                </div>
                <div class="col-xs-6 text-right">
                  <label class="checkbox-inline"><input type="checkbox" id="freesignin">一周内免登录</label>
                </div>
              </div>
              <p>
                <a href="javascript:;">忘记密码？</a>
              </p>
            </form>

            <div class="form-horizontal" id="userForm">
              <div class="form-group">
                <div class="col-xs-12">
                  您好，<a href="/{{ site.subDomain }}/percenter" class="username"></a>，您已经登陆。
                </div>
              </div>
              <p>
                <a href="javascript:loginOut();">注销</a>
              </p>
            </div>
          </div>
        </div>
      </div>
      {{include 'components/footer'}}

    <script>
        $('#loginForm').validator().on('submit', function (e) {
          if (e.isDefaultPrevented()) {
            // handle the invalid form...
            console.log('handle the invalid form...');
          } else {
            // everything looks good!
            console.log('everything looks good!');
            var username = $('#username').val(),
            password = $('#password').val(),
            freesignin = $('#freesignin').is(':checked');

            $.ajax({
                type: "post",
                url: '{{site_addr}}/login?q={"query":{"username":"'+username+'","password":"'+password+'"}}',

                beforeSend: function (XMLHttpRequest) {
                    //ShowLoading(); // TODO
                },
                success: function (data, textStatus) {
                  console.log('post data: ');
                  console.log(data);

                  // return;
                   if(data && data.token) {
                        // bootbox.alert({
                        //    size: "small",
                        //    message: '登陆成功'
                        // });
                        localStorage.setItem('tengpu_jwt_token', data.token); // 存储jwt

                        // checkLoginStatus();

                        setTimeout(function () {
                          location.href = '/'+CITY+'/percenter' // JWT
                        }, 100);
                   } else {
                        bootbox.alert({
                           size: "small",
                           message: '输入的用户或密码有误'
                        });
                   }
                },
                complete: function (XMLHttpRequest, textStatus) {
                    //HideLoading();
                },
                error: function () {
                    //请求出错处理
                }
            });
          }

          return false;
        });

        $('#password').keypress(function (evt) {
          if (13 == evt.keyCode) {
            signin();
          }
        });

        function signin() {
            $('#loginForm').submit();
            return false;
        }

        // 有登陆用户的回调函数
        loginCb && loginCb.push(function () {
          console.log('loginUser: ', loginUser);

          $('#userForm').show();
          $('#userForm').find('.username').text(userName);
          $('#loginForm').hide();
        });
    </script>

</body>
</html>
